//
//  EmptyView.swift
//  FeastBooking
//
//  Created by Imon on 2017/12/9.
//  Copyright © 2017年 Imon. All rights reserved.
//

import UIKit

protocol EmptyViewDelegate {
    func reload()
}

class EmptyView: UIView {

    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */
    
    var delegate: EmptyViewDelegate?

    let label: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.gray
        label.textAlignment = .left
        label.font = AppFont(size: 16)
        label.numberOfLines = 0
        label.text = LanguageHelper.getString(key: "NO_RESULT")
        return label
    }()
    
    let button: UIButton = {
        let bt = UIButton()
        bt.contentHorizontalAlignment = .right
        bt.setTitle(LanguageHelper.getString(key: "RELOAD"), for: .normal)
        bt.titleLabel?.font = AppFont(size: 13)
        bt.setTitleColor(proceededColor, for: .normal)
        return bt
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = lightSaperatorColor
        self.addSubview(label)
        self.addSubview(button)
        button.addTarget(self, action: #selector(tapReload(_:)), for: .touchUpInside)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func tapReload(_ sender: UIButton) {
        delegate?.reload()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let textMaxSize = CGSize(width: bounds.width, height: bounds.height)
        let labelSize = TextSize.textSize(text: label.text!, font: label.font, maxSize: textMaxSize)
        let buttonSize = TextSize.textSize(text: (button.titleLabel?.text)!, font: (button.titleLabel?.font)!, maxSize: textMaxSize)
        label.frame = CGRect(x: (self.frame.width - labelSize.width) / 2, y: (self.frame.height - labelSize.height) / 2, width: labelSize.width, height: labelSize.height)
        button.frame = CGRect(x: (self.frame.width - buttonSize.width) / 2, y: label.frame.origin.y + label.frame.height + 15, width: buttonSize.width, height: buttonSize.height)
    }
}
